package com.lhj.xxsc.seckill.controller;

import com.lhj.xxsc.seckill.service.SeckillService;
import com.lhj.xxsc.seckill.to.SecKillSkuRedisTo;
import com.xxsc.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 罗惠江
 * @version 1.0
 * @email 1511619294@qq.com
 * @date 2021/1/13 16:57
 */
@Controller
public class SeckillController {

	@Autowired
	SeckillService seckillService;

	/**
	 * 返回当前时间可以参与的秒杀商品信息
	 *
	 * @return
	 */
	@ResponseBody
	@GetMapping("/currentSeckillSkus")
	public R getCurrentSeckillSkus() {
		System.out.println("查看秒杀商品！");
		List<SecKillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
		return R.ok().setData(vos);
	}

	/**
	 * 获取商品的秒杀信息
	 *
	 * @param skuId
	 * @return
	 */
	@ResponseBody
	@GetMapping("/sku/seckill/{skuId}")
	public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
		System.out.println("查看"+skuId+"详情");
		SecKillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
		return R.ok().setData(to);
	}

	@GetMapping("/kill")
	public String secKill(@RequestParam("killId") String killId, @RequestParam("key") String key, @RequestParam("num") Integer num, Model model) {
		//判断是否登录
		String orderSn = seckillService.kill(killId, key, num);
		model.addAttribute("orderSn", orderSn);
		return "success";
	}
}
